package com.biz.service.MService.merchant;

import com.biz.model.Hmodel.base.TBaseBrand;
import com.biz.model.Hmodel.base.TWxGoods;
import com.biz.model.Pmodel.order.PorderDetail;
import com.biz.model.common.MessageBox;
import com.biz.service.HService.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lzq
 */
@Service("merchantService")
public class MerchantServiceImpl extends BaseServiceImpl<TWxGoods> implements MerchantServiceI {
  @Resource(name = "daoSupport")
  private DaoSupport dao;

  @Autowired
  private BaseDaoI<TBaseBrand> tBaseBrandDao;

  @Override
  public MessageBox doLoginByPwd(String loginName, String pwdMd5) {

    MessageBox box = new MessageBox();
    box.setMsg("登录失败！");
    box.setSuccess(false);

    Map param = new HashMap();
    param.put("loginName", loginName);
    param.put("pwdMd5", pwdMd5);

    try {
      List<String> mercantS = (List<String>)dao.findForList("MerchantDao.getMerchantCount",param);
      if(mercantS != null)
      {
        if(mercantS.size() == 1){
          box.setMsg(mercantS.get(0).toString());
          box.setSuccess(true);
        }else if(mercantS.size() == 0){
          box.setMsg("用户名或密码错误！");
          box.setSuccess(false);
        }else {
          box.setMsg("重复用户，请联系管理员！");
          box.setSuccess(false);
        }
      }else{
        box.setMsg("用户名或密码错误！");
        box.setSuccess(false);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return box;
  }

  @Override
  public TBaseBrand getTbaseBrandById(String merchantId){
    return tBaseBrandDao.getById(TBaseBrand.class,merchantId);
  }

  @Override
  public List<Map<String,Object>> getRecordByMerchantId(String merchantId)throws Exception{

    return (List<Map<String,Object>>)dao.findForList("MerchantDao.loadRecord",merchantId);
  }


  @Override
  public synchronized MessageBox merchantDoCharge(String merchantId, String code){
    MessageBox box = new MessageBox();
    box.setMsg("兑换失败！");
    box.setSuccess(false);

    Map param = new HashMap();
    param.put("merchantId", merchantId);
    param.put("code", code);

    try {
      //判断商品是否可以核销
      PorderDetail porderDetail =
              (PorderDetail)dao.findForObject("MerchantDao.getPorderDetailByCode",code);
      if(porderDetail != null )
      {
        if(1 == porderDetail.getState().intValue())
        {
          if(porderDetail.getBrandCode() !=null && merchantId.equals(porderDetail.getBrandCode()))
          {
            //兑换
            String detailId = porderDetail.getId();
            dao.update("MerchantDao.updateDetailState",detailId);
            box.setMsg("兑换成功！");
            box.setSuccess(true);
          }else{
            box.setMsg("请前往对应商家！");
            box.setSuccess(false);
          }
        }else{
          box.setMsg("请勿重复兑换！");
          box.setSuccess(false);
        }
      }else{
        box.setMsg("商品不存在！");
        box.setSuccess(false);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return box;
  }
}
